package com.group.three.controller.orderController;

import com.github.pagehelper.PageInfo;
import com.group.three.entity.myOrder.OrderBuyerInformation;
import com.group.three.entity.myOrder.OrderLog;
import com.group.three.entity.myOrder.OrderSearchParams;
import com.group.three.entity.myOrder.OrderSimpleVO;
import com.group.three.service.myorderservice.OrderBuyerService;
import com.group.three.vo.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@Api(tags = "买家端,订单接口")
@RequestMapping("/buyer/order")
public class OrderBuyerController {
    @Autowired
    private OrderBuyerService orderBuyerService;

    @ApiOperation(value = "查询会员订单列表")
    @GetMapping("/order")
    public JsonResult order(OrderSearchParams orderSearchParams) {
        int pageNumber = orderSearchParams.getPageNumber();
        int pageSize = orderSearchParams.getPageSize();
        String memberId = orderSearchParams.getMemberId();
        String keywords = orderSearchParams.getKeywords();
        //订单状态('全部订单', '待付款', '待收货', '已完成')
        String tag = orderSearchParams.getTag();
        List<OrderBuyerInformation> orderBuyerInformations = orderBuyerService.selectBuyerOrder(memberId, pageNumber, pageSize, tag,keywords);
        PageInfo<OrderBuyerInformation> pageInfo = new PageInfo<>(orderBuyerInformations);
        long total = pageInfo.getTotal();
        Map<String, Object> buyerOrder = new HashMap<>();
        buyerOrder.put("records", orderBuyerInformations);
        buyerOrder.put("total", total);
        if (orderBuyerInformations.size() != 0) {
            return new JsonResult("200", "数据查询成功", buyerOrder);
        }
        return new JsonResult("400", "没有数据");
    }

    /**
     * 查询订单详情信息
     *
     * @param orderSn 订单编号
     * @return
     */
    @ApiOperation(value = "查询订单详情信息")
    @GetMapping("/order/{orderSn}")
    public JsonResult getOrderDetail(@PathVariable("orderSn") String orderSn) {
        //查询订单详情
        List<OrderSimpleVO> orderItem = orderBuyerService.selectBuyerOrderItem(orderSn);
        //订单日志
        List<OrderLog> orderLogs = orderBuyerService.selectBuyerOrderItemLog(orderSn);
        Map<String, Object> OrderItem = new HashMap<>();
        OrderItem.put("order", orderItem);
        OrderItem.put("orderLogs", orderLogs);
        if (orderItem.size() != 0) {
            return new JsonResult("200", "数据查询成功", OrderItem);
        }
        return new JsonResult("400", "没有数据");
    }

    /**
     * 确认收货
     *
     * @param orderSn 订单号
     */
    @ApiOperation(value = "确认收货")
    @GetMapping("/order/{orderSn}/receiving")
    public JsonResult sureReceived(@PathVariable("orderSn") String orderSn) {
        int i = orderBuyerService.sureReceived(orderSn);
        if (i == 1) {
            return new JsonResult("200", "数据查询成功", "success");
        }
        return new JsonResult("400", "确认收货失败");
    }

    /**
     * 取消订单
     *
     * @param orderSn 订单号
     * @param reason  取消原因
     * @return
     */
    @ApiOperation(value = "取消订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderSn", value = "订单编号", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "reason", value = "取消原因", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping(value = "/order/{params.orderSn}/cancel")
    public JsonResult cancel(@RequestParam String orderSn, @RequestParam String reason) {
        int i = orderBuyerService.cancelOrder(orderSn, reason);
        if (i == 1) {
            return new JsonResult("200", "订单取消成功");
        }
        return new JsonResult("400", "订单取消失败");

    }


    @ApiOperation(value = "删除订单")
    @DeleteMapping(value = "/delete/{orderSn}")
    public JsonResult deleteOrder(@PathVariable String orderSn) {
        int i = orderBuyerService.deleteOrder(orderSn);
        if (i == 1) {
            return new JsonResult("200", "订单删除成功");
        }
        return new JsonResult("400", "订单删除失败");
    }

}
